﻿using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif

using Game.AIBehaviorTree;

public static class BTNodeUtils
{
    public static void CreateNewGraph(string wantedName, string treeDes)
    {
       BTreeEditorData.instance.newTree(wantedName, treeDes);
    }

    public static void UnloadGraph()
    {
        BTNodeEditorWindow curWindow = EditorWindow.GetWindow<BTNodeEditorWindow>();
        if (curWindow != null)
        {
            curWindow.curGraph = null;
        }
    }

    public static bool LoadGraph(string treeName)
    {
        BTNodeGraph curGraph = null;
        curGraph = BTreeEditorData.instance.getBTreePath(treeName);
        if(curGraph == null)
        {
            EditorUtility.DisplayDialog("node message","Unable to load selected graph","OK");
            return false;
        }
        else
        {
            BTNodeEditorWindow curWindow = EditorWindow.GetWindow<BTNodeEditorWindow>();
            if (curWindow != null)
            {
                curWindow.curGraph = curGraph;
                return true;
            }
        }
        return false;
    }

    public static void CreateNode(BTNodeGraph curGraph,BTNodeBase parentNode, string nodeType, Vector2 mousePos)
    {
        if (curGraph == null)
        {
            return;
        }

        BTNodeBase curNode = null;
        curNode = ScriptableObject.CreateInstance<BTNodeBase>();

        
        //curNode.nodeName = nodeName;
        if (curNode != null)
        {
            //创建树node
            curNode.InitNode(curGraph, nodeType, null);
            if (parentNode != null)
            {
                parentNode.addChild(curNode);
            }
            curNode.nodeRect.x = mousePos.x;
            curNode.nodeRect.y = mousePos.y;
            UtilsEditor.CreateAsset(curNode, curNode.showName + ".asset", BTreeEditorData.pathTreeEditorData + curGraph.GraphName + "/");
            EditorUtility.SetDirty(curGraph);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
    }

    public static void CreateNode(BTNodeGraph curGraph,NodeType nodeType,Vector2 mousePos)
    {
        if(curGraph == null)
        {
            return;
        }

        BTNodeBase curNode = null;
        switch(nodeType)
        {
            case NodeType.Float:
                curNode = ScriptableObject.CreateInstance<BTFloatNode>();
                curNode.nodeName = "float Node";
                break;
            case NodeType.Add:
                curNode = ScriptableObject.CreateInstance<BTAddNode>();
                curNode.nodeName = "Add Node";
                break;
            default:
                break;
        }

        if(curNode != null)
        {
            curNode.InitNode();
            curNode.nodeRect.x = mousePos.x;
            curNode.nodeRect.y = mousePos.y;
            curNode.parentGraph = curGraph;
            curGraph.nodes.Add(curNode);

            AssetDatabase.AddObjectToAsset(curNode, curGraph);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
    }

    ///复制节点
    public static BTNodeBase copyNode(BTNodeBase node, BTNodeGraph newGraph)
    {
        return node.copyAllChildren(null, newGraph);
    }

    /// <summary>
    /// 在该子节点前插入节点
    /// </summary>
    /// <param name="prenode">如果为空则为普通插入</param>
    /// <param name="node"></param>
	public static void InsertChildBefore( BTNodeBase prenode , BTNodeBase node )
	{
        prenode.nodeParent.InsertChildBefore(prenode,node);
    }

    ///删除节点及其子节点
    public static void removeNode(BTNodeBase node,BTNodeGraph curGraph)
    {
        if(curGraph == null && node == null)
        {
            return;
        }

        node.removeSelf();

    }

    public static void DrawGrid(Rect viewRect,float gridSpacing,float gridOpacity,Color gridColor)
    {
        int widthDivs = Mathf.CeilToInt(viewRect.width / gridSpacing);
        int heightDivs = Mathf.CeilToInt(viewRect.height / gridSpacing);

        Handles.BeginGUI();
        Handles.color = new Color(gridColor.r, gridColor.g, gridColor.b, gridOpacity);

        for(int x = 0; x < widthDivs; x++)
        {
            Handles.DrawLine(new Vector3(gridSpacing * x, 0f, 0f),
                             new Vector3(gridSpacing * x, viewRect.height, 0f));
        }

        for (int y = 0; y < heightDivs; y++)
        {
            Handles.DrawLine(new Vector3(0f, gridSpacing * y, 0f),
                             new Vector3(viewRect.width, gridSpacing * y, 0f));
        }
        Handles.color = Color.white;
        Handles.EndGUI();
    }
}
